package com.study.demo.controller;

import com.study.demo.exceptions.UserNotExistException;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class ErrorHandle {
//    @ResponseBody
//    @ExceptionHandler(UserNotExistException.class)
//    public Map<String,Object> errorHandle(Exception e){
//        Map<String,Object> map=new HashMap<>();
//        map.put("code","user.notExist");
//        map.put("message","用户不存在");
//        return map;
//    }

    @ExceptionHandler(UserNotExistException.class)
    public String errorHandle(Exception e, HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        request.setAttribute("javax.servlet.error.status_code",400);
        map.put("code","user.notExist");
        map.put("message","用户不存在");
        request.setAttribute("ext",map);
        return "forward:/error";
    }
}
